////////////////////////////////////////////////////////////////////////////////
// This file is part of the Mist compiler.                                    //
//                                                                            //
// Copyright (C) 2009 The Mist project                                        //
//                                                                            //
// For a list of contributors, see the COMMITTERS file.                       //
//                                                                            //
// The Mist compiler is free software: you can redistribute it and/or modify  //
// it under the terms of the GNU General Public License as published by       //
// the Free Software Foundation, either version 3 of the License, or          //
// (at your option) any later version.                                        //
//                                                                            //
// The Mist compiler is distributed in the hope that it will be useful,       //
// but WITHOUT ANY WARRANTY; without even the implied warranty of             //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              //
// GNU General Public License for more details.                               //
//                                                                            //
// You should have received a copy of the GNU General Public License          //
// in the COPYING file. If not, see <http://www.gnu.org/licenses/>.           //
////////////////////////////////////////////////////////////////////////////////

///
/// \file
/// Contains the implementation of the set_parents function.
///

#include "setparents.hpp"

////////////////////////////////////////////////////////////////////////////////
// Includes                                                                   //
////////////////////////////////////////////////////////////////////////////////

#include <stack>
using std::stack;

#include "navigator.hpp"
#include "tree.hpp"

////////////////////////////////////////////////////////////////////////////////
// SetParentsVisitor Class                                                    //
////////////////////////////////////////////////////////////////////////////////

class SetParentsVisitor : public Navigator {
	public:
		
		VISIT_FUNCTIONS_FROM( CONCRETE_NODES )
	
	private:
		
		stack<wptr<Node> > _parentNode;
};

////////////////////////////////////////////////////////////////////////////////
// Macro Definitions                                                          //
////////////////////////////////////////////////////////////////////////////////

#define CONCRETE_NODE_AS_SET_PARENT_VISIT_FUNCTION(r, data, elem) \
	void SetParentsVisitor::visit(wptr<elem> node) {              \
		assert(node);                                             \
		                                                          \
		if (!_parentNode.empty())                                 \
			node->parent = _parentNode.top();                     \
		                                                          \
		_parentNode.push(node);                                   \
		                                                          \
		Navigator::visit(node);                                   \
		                                                          \
		_parentNode.pop();                                        \
	}

#define SET_PARENT_VISIT_FUNCTIONS_FROM(nodes) \
	BOOST_PP_SEQ_FOR_EACH(CONCRETE_NODE_AS_SET_PARENT_VISIT_FUNCTION,, nodes)

////////////////////////////////////////////////////////////////////////////////
// Function Implementations                                                   //
////////////////////////////////////////////////////////////////////////////////

void set_parents(wptr<Node> astNode) {
	assert(astNode);
	
	sptr<SetParentsVisitor> visitor(new SetParentsVisitor);
	
	astNode->accept(visitor);
}

////////////////////////////////////////////////////////////////////////////////

SET_PARENT_VISIT_FUNCTIONS_FROM( CONCRETE_NODES )
